package acs.bridge;

import acs.bridge.model.bm_file_model;

import java.io.*;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Pattern;

/**
 * acs.bridge
 * User: KuznetsovStV
 * Date: 26.07.13
 * Time: 15:31
 * Загрузка файлов с диска
 */
public class bm_load_file_path implements bm_load_file_interface {

    org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(this.getClass().getName());

    private String srcPath;
    private String pattern;


    public String getSrcPath() {
        return srcPath;
    }

    public void setSrcPath(String srcPath) {
        this.srcPath = srcPath;
    }

    public String getPattern() {
        return pattern;
    }

    public void setPattern(String pattern) {
        this.pattern = pattern;
    }

    @Override
    public void get_file(String a_file_name, String a_dst_path) throws BridgeException {

        File src_dir_file = new File(srcPath + a_file_name);
        File dst_dir_file = new File(a_dst_path + a_file_name);

        InputStream inStream = null;
        OutputStream outStream = null;
        try {
            inStream = new FileInputStream(src_dir_file);
            outStream = new FileOutputStream(dst_dir_file);

            byte[] buffer = new byte[1024];

            int length;
            //copy the file content in bytes
            while ((length = inStream.read(buffer)) > 0) {
                outStream.write(buffer, 0, length);
            }

            inStream.close();
            outStream.close();
        } catch (IOException e) {
            logger.error(e.getMessage() + " (PathException)");
            logger.error(e.getStackTrace());
            throw new BridgeException(e.getMessage());
        }


    }

    @Override
    public List<bm_file_model> get_directory_list() {


        List<bm_file_model> l_dir_list = new ArrayList<bm_file_model>();

        logger.info("Start load processing path=" + getSrcPath() + "}");
        File src_dir_file = new File(getSrcPath());


        SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyy_HHmmssZ");
        sdf.setTimeZone(TimeZone.getTimeZone("GMT-00:00"));
        SimpleDateFormat sd_z_date = new SimpleDateFormat("ddMMyyyy_HHmmss");
        sd_z_date.setTimeZone(TimeZone.getTimeZone("GMT-00:00"));
        File[] fileList = src_dir_file.listFiles(new FilenameFilter() {
            private Pattern pattern = Pattern.compile(getPattern());

            public boolean accept(File dir, String name) {
                return Pattern.matches(getPattern(),name);

                       // pattern.matcher(name).matches();
            }
        });


        for (int i = 0; i <= fileList.length - 1; i++) {

            String l_filename = fileList[i].getName().toUpperCase();
            String fr_id = l_filename + "_" + sdf.format(new Date(fileList[i].lastModified())) + "_" + fileList[i].length();
            bm_file_model l_bm_file_model = new bm_file_model();
            l_bm_file_model.setFr_id(fr_id);
            l_bm_file_model.setFile_name(l_filename);
            l_bm_file_model.setFile_path(getSrcPath());
            l_bm_file_model.setLast_modified(sd_z_date.format(new Date(fileList[i].lastModified())));
            l_bm_file_model.setFile_size(new BigDecimal(fileList[i].length()));
            l_dir_list.add(l_bm_file_model);
        }
        logger.info("End load processing.");
        return l_dir_list;
    }


}
